﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ET.Entity;
using ET.DataAccess;
using ET.Utility;

public partial class Web_Registry : System.Web.UI.Page
{
    DaServiceTracking daServiceTracking = new DaServiceTracking();
    DaServiceInfo daServiceInfo = new DaServiceInfo();
    public string employeeId = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownListBind();
            if (SessionManager.User != null)
            {
                lblName.Text = SessionManager.User.EmployeeName;
                List<EtServiceTracking> serviceTrackingSet = 
                    daServiceTracking.GetServiceTrackingByUserId(SessionManager.User.Id.ToString());
                List<EtServiceTracking> serviceTrackingWithDurationSet = serviceTrackingSet;
                for (int i = 0; i < serviceTrackingSet.Count; i++)
                {
                    if (i + 1 < serviceTrackingSet.Count)
                    {
                        serviceTrackingSet[i].Duration =
                            (serviceTrackingSet[i].RegisterTime - serviceTrackingSet[i + 1].RegisterTime).ToString(@"dd\.hh\:mm\:ss");
                    }
                    else
                    {
                        serviceTrackingSet[i].Duration =
                               (serviceTrackingSet[i].RegisterTime - serviceTrackingSet[i].RegisterTime).ToString(@"dd\.hh\:mm\:ss");
                    }
                }
                rpt.DataSource = serviceTrackingWithDurationSet;
                rpt.DataBind();
                //GridView1.DataSource = serviceTrackingSet;
                //GridView1.DataBind();
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtClient.Text))
        {
            lblInfo.Text = "Must type client's name!";
        }
        else
        {
            EtServiceTracking serviceTracking = new EtServiceTracking()
            {
                ClientName = txtClient.Text,
                EmployeeId = SessionManager.User.Id,
                ServiceId = Guid.Parse(ddlService.SelectedValue),
                Ip = Request.UserHostAddress,
                Note = txtNote.Text,
                RegisterTime = System.DateTime.Now.AddHours(2)
            };
            daServiceTracking.Insert(serviceTracking);
            lblInfo.Text = "Submit successful!";
            txtClient.Text = string.Empty;
            txtNote.Text = string.Empty;
            Response.Redirect("Registry.aspx");
        }
    }

    protected void DropDownListBind()
    {
        List<EtServiceInfo> serviceTrackingSet = daServiceInfo.GetAllUnDeleted();
        ddlService.DataSource = serviceTrackingSet;
        ddlService.DataValueField = "Id";
        ddlService.DataTextField = "ServiceName";
        ddlService.DataBind();
    }
}